package cqrtplm.listener;

import cn.hutool.extra.spring.SpringUtil;
import com.hustcad.plm.pdm.workflow.annotion.CustomListener;
import cqrtplm.service.OaApiService;
import cqrtplm.service.impl.WorkflowMappingServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.delegate.ExecutionListener;
import org.flowable.engine.runtime.Execution;

import javax.annotation.Resource;
import java.math.BigInteger;

/**
 * $ 方法的功能描述.
 *
 * @param $ $
 * @author Administrator$
 * @return $ 的说明
 * @throws $ 可能抛出的异常
 * @date 2025/11/11$ 下午9:39$
 * @since 1.0
 */
@Slf4j
@CustomListener(category = "润通", description = "查询OA流程状态(修改全局变量):flowState为true")
public class RTCheckOAFlowStateListener  implements ExecutionListener {
    @Override
    public void notify(DelegateExecution execution) {
        try {
            log.info("开始执行监听 ==> 查询OA流程状态(修改全局变量)");
            String workFlowContainerId = execution.getProcessInstanceBusinessKey();
            if (StringUtils.isBlank(workFlowContainerId)) {
                workFlowContainerId = execution.getVariable("workFlowContainerId").toString();
            }
            RuntimeService runtimeService = SpringUtil.getBean(RuntimeService.class);
            Execution execution1 = runtimeService.createExecutionQuery().processInstanceBusinessKey(workFlowContainerId)
                    .singleResult();
            OaApiService oaApiService = SpringUtil.getBean(OaApiService.class);
            Boolean flag = oaApiService.checkOAFlowState(new BigInteger(workFlowContainerId));
            runtimeService.setVariable(execution1.getId(), "flowState", flag);
            log.info("正常执行监听结束 <== 查询OA流程状态(修改全局变量)");
        } catch (Exception e) {
            log.error("监听RTCheckOAFlowStateListener异常：", e);
            throw e;
        }
    }
}
